const express = require('express');
const app = express();
const router = require('./api/api');
const token = require('./token/token');


//引入路由配置
app.use(router);
//验证token
app.use(token.yan(['/','/login','/editAddress']));

/*
错误处理中间件 
*/
app.use( (err,req,res,next)=>{
    if(err){
        console.log(err.name);
        if(err.name=='UnauthorizedError'){
            let obj ={
                code:0,
                msg:'无效token',
            };
            res.send(obj);
        }else{
            let obj ={
                code:0,
                msg:'未知错误',
            };
            res.send(obj);
        }
    }else{
        
        next();
    }
} );
app.listen(10086,()=>{
    console.log('服务启动成功');
});